package com.example.pdfdemo.controller;


import org.apache.commons.io.FileUtils;
import org.apache.pdfbox.pdmodel.PDDocument;
import org.apache.pdfbox.pdmodel.PDDocumentInformation;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.net.URLEncoder;

@Controller
public class PdfController {


    /**
     * 展示pdf
     * @param response
     * @throws IOException
     */
    @RequestMapping("/getPdf")
    public void getPdf(HttpServletResponse response) throws IOException {
        File file = new File("src/main/resources/泰山版.pdf");
        if (file.exists()) {
            filename=  filename.replaceAll("\\.pdf", " ").replaceAll("\\.PDF", "");
            response.setCharacterEncoding("utf-8");
            response.setContentType("application/pdf");
            response.setHeader("Content-Disposition", "inline;fileName=" + filename + ";fileName*=UTF-8''" + filename);
            DataOutputStream temps = new DataOutputStream(response.getOutputStream());
            temps.write(FileUtils.readFileToByteArray(file));
            PDDocument document = PDDocument.load(file);
            PDDocumentInformation info = document.getDocumentInformation();
            //设置PDF文档属性对象的文件名称（最重要的环节）
            info.setTitle(filename);
            document.setDocumentInformation(info);
            //修改完直接输出到响应体中
            document.save(temps);
            temps.close();
        } else {
            System.err.println("文件不存在!");
        }
    }
















    /**
     * 展示pdf 文件名称处理 前端
     * @param response
     * @throws IOException
     */
    @RequestMapping("/PDF/{fileName}")
    public void PDF(@PathVariable String fileName,HttpServletResponse response) throws IOException {
        File file = new File("src/main/resources/泰山版.pdf");
        if (file.exists()) {
            response.setCharacterEncoding("utf-8");
            response.setContentType("application/pdf");
            DataOutputStream temps = new DataOutputStream(response.getOutputStream());
            temps.write(FileUtils.readFileToByteArray(file));
            temps.close();
        } else {
            System.err.println("文件不存在!");
        }
    }










    /**
     * 展示pdf 文件名称处理 后端
     * @param response
     */
    @RequestMapping("preview")
    public void preview(HttpServletResponse response) {
        try {
            File file = new File("src/main/resources/泰山版.pdf");
            byte[] pdfByte = FileUtils.readFileToByteArray(file); //从本地读取一个pdf文件做测试
            String pdfName = "自定义PDF标题.PDF";


            response.reset();
            response.setContentType("application/pdf;charset=UTF-8");
            response.setHeader("Content-Disposition", "inline;filename=" + URLEncoder.encode(pdfName, "UTF-8"));//这里的名字并不起作用
            OutputStream out = response.getOutputStream();



            PDDocument document = PDDocument.load(pdfByte); //加载pdf
            PDDocumentInformation info = document.getDocumentInformation(); //获得文档属性对象
            info.setTitle(pdfName); //修改标题属性  这个标题会被展示
            document.setDocumentInformation(info);
            document.save(out); //修改完直接输出到响应体中
            document.close();

            out.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }



































}
